#ifndef SERVER_H
#define SERVER_H

#include <QTcpSocket>
#include <QtNetwork>
#include <stdlib.h>

QT_BEGIN_NAMESPACE
class QTcpServer;
QT_END_NAMESPACE

class Server : public QObject
{
    Q_OBJECT
public:
    explicit Server();

signals:
    /**
     * @brief serverStarted Signal is emitted when server starts to report it in main window.
     * @param msg Server status.
     */
    void serverStarted(QString msg);

    /**
     * @brief clientConnected Signal is emitted when client connects to our server to report it in main window.
     * @param client String identifying a client.
     */
    void clientConnected(QString client);

    /**
     * @brief clientDisconnected Signal is emitted when client disconnects from our server to report it in main window.
     * @param client String identifying a client.
     */
    void clientDisconnected(QString client);

    /**
     * @brief logMsg Signal is emitted when sth has to be logged in main window.
     * @param msg Message to log.
     */
    void logMsg(QString msg);

private slots:

    /**
     * @brief run Starts the server.
     */
    void run();

    /**
     * @brief acceptClientConnection Accepts new client connection.
     */
    void acceptClientConnection();

    /**
     * @brief removeClientConnection Removes client connection.
     */
    void removeClientConnection();

    /**
     * @brief logMessage Slot executed when one of receiving threads recevied some message.
     * @param msg Message to log.
     */
    void logMessage(QString msg);

    void connectionError(QAbstractSocket::SocketError err);

private:

    /**
     * @brief tcpServer Server socket starts listening after calling run() method.
     */
    QTcpServer *tcpServer;

    /**
     * @brief clientConnections Current client sockets.
     */
    QList<QTcpSocket*> clientConnections;

};


#endif // SERVER_H
